﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Altbrot.Yawr.Buslogic;
using Altbrot.Yawr.ShoutCast;
using Altbrot.Yawr.Entities;
using System.Reflection;

namespace UpdateDB
{
    class Program
    {
        static void Main(string[] args)
        {
            string _baseUri = "http://www.shoutcast.com/sbin/newxml.phtml?rss=1";
            string _tmpFilePath = "tunein-station.pls";
            string connectionString = @"C:\Users\altbrot\work\carpc\ModuleDevelopment\Yawr\Altbrot.Yawr.Plugin\Database\FeedItemDb.sdf";

            string filePath = System.IO.Path.Combine(
                System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), _tmpFilePath);
            Uri baseUri = new Uri(_baseUri);

            ShoutCastManager scm = new ShoutCastManager(baseUri, filePath, new OnlineBuslogicManager());

            IBuslogicManager man = new DatabaseBuslogicManager(connectionString);
            ShoutCastManager scmDb = new ShoutCastManager(baseUri, filePath, man);

            List<FeedItem> baseStepList = scmDb.GetBaseStep(48);
            int index = 0;

            foreach (FeedItem genre in baseStepList)
            {
                index++;
                Console.WriteLine(String.Format("Genre: {0} - {1} of {2}", genre.Title, index, baseStepList.Count));
                scm.SelectedFeedItem = genre;

                List<FeedItem> stationList = new List<FeedItem>();

                if (scm.GetNextStep(out stationList))
                {
                    man.UpdateStationList(stationList, genre.Title, _tmpFilePath, null);
                }

                List<FeedItem> gl = new List<FeedItem>();
                gl.Add(genre);
                man.UpdateGenreList(gl);

                scm = new ShoutCastManager(baseUri, filePath, new OnlineBuslogicManager());
            }
        }
    }
}
